import pandas as pd
import numpy as np

from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV

#竞赛的评价指标为logloss


#SVM并不能直接输出各类的概率，所以在这个例子中我们用正确率作为模型预测性能的度量
from sklearn.metrics import accuracy_score

from matplotlib import pyplot

train = pd.read_csv("FE_pima-indians-diabetes.csv")
y_train = train['Target']
X_train = train.drop(["Target"], axis=1)

#线性SVM


#由于速度较慢，这里只调整了超参数C，没调L1/L2正则函数
Cs = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
param_grid = {'C': Cs}
grid = GridSearchCV(SVC(kernel='linear'), param_grid, cv=5, n_jobs = 4)

grid.fit(X_train, y_train)
print(grid.best_score_)
print(grid.best_params_)


#RBF核SVM正则参数调优
Cs1 = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
gammas1 = [0.0001,0.001, 0.01, 0.1, 1]
#gammas =[1e-5, 1e-6]
param_grid = {'C': Cs1, 'gamma' : gammas1}
grid = GridSearchCV(SVC(kernel='rbf'), param_grid, cv=5, n_jobs = 4)

grid.fit(X_train, y_train)
print(grid.best_score_)
print(grid.best_params_)

# plot CV误差曲线
test_means = grid.cv_results_['mean_test_score']
test_stds = grid.cv_results_['std_test_score']

# plot results
n_Cs = len(Cs1)
number_gamms = len(gammas1)

test_scores = np.array(test_means).reshape(n_Cs, number_gamms)
# train_scores = np.array(train_means).reshape(n_Cs,number_penaltys)
test_stds = np.array(test_stds).reshape(n_Cs, number_gamms)
# train_stds = np.array(train_stds).reshape(n_Cs,number_penaltys)

x_axis = np.log10(Cs1)
for i, value in enumerate(gammas1):
    pyplot.plot(x_axis, test_scores[:, i], label=gammas1[i])


pyplot.legend()
pyplot.xlabel('log(C)')
pyplot.ylabel('accuary')
pyplot.savefig('SVMGridSearchCV_C.png')

pyplot.show()